﻿using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace Flight.Controller.Entities
{
    /// <summary>
    /// Represents an engine entity.
    /// </summary>
    public class Engine : IDisposable
    {
        private byte _maxLoad = 100;
        private byte _current = 0;

        /// <summary>
        /// Initializes a new instance of the <see cref="Engine"/> class.
        /// </summary>
        /// <param name="maxLoad">Max load that can be apply to engine.</param>
        /// <param name="position">Engine position.</param>
        /// <param name="propOrientation">Engine propeller rotation orientation.</param>
        /// <param name="escPwm">Engine ESC PWM.</param>
        /// <param name="type">Engine ESC type.</param>
        public Engine(byte maxLoad, EnginePosition position, PropellerRotationOrientation propOrientation, PWM escPwm, ControllerType type)
        {
            MaxLoad = maxLoad;
            Position = position;
            RotationOrientation = propOrientation;
            SpeedControllerPWM = escPwm;
            EscType = type;
        }

        ~Engine()
        {
            if (SpeedControllerPWM != null)
                SpeedControllerPWM.Dispose();
        }

        /// <summary>
        /// Gets or sets the max load that can be applied to engine.
        /// </summary>
        public byte MaxLoad
        {
            get
            {
                return _maxLoad;
            }
            set
            {
                if (value > 100)
                    throw new ArgumentException("Invalid value, maximum is 100");

                _maxLoad = value;
            }
        }

        /// <summary>
        /// Gets or sets the current engine throttle.
        /// </summary>
        public byte CurrentThrottle
        {
            get
            {
                return _current;
            }
            set
            {
                if (value > 100)
                    throw new ArgumentException("Invalid value, maximum is 100");

                _current = value;
            }
        }

        /// <summary>
        /// Gets or sets the engine propeller orientation.
        /// </summary>
        public PropellerRotationOrientation RotationOrientation { get; set; }

        /// <summary>
        /// Gets or sets the engine postion.
        /// </summary>
        public EnginePosition Position { get; set; }

        /// <summary>
        /// Gets or sets the ESC board PWM pin for this engine.
        /// </summary>
        public PWM SpeedControllerPWM { get; set; }

        /// <summary>
        /// Gets or sets the ESC communication type.
        /// </summary>
        public ControllerType EscType { get; set; }

        public void Dispose()
        {
            SpeedControllerPWM.Dispose();
        }
    }

    public enum PropellerRotationOrientation
    {
        CLOCKWISE = 1,
        COUNTERCLOCKWISE = 2
    }

    public enum ControllerType
    {
        PWM = 1,
        PPM = 2
    }
}
